package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.demo.dao.TbMenuDao;
import com.example.demo.entity.TbMenu;
import com.example.demo.entity.TreeData;
import com.example.demo.service.TbMenuService;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hsn
 * @className wuyp
 * @description:
 * @Date 2022/4/2 10:05
 **/
@Service
@Transactional(rollbackOn = Exception.class )
public class TbMenuServiceImpl implements TbMenuService {
    @Autowired
    private TbMenuDao tbMenuDao;
    @Override
    public List<TbMenu> findAll() {

        return tbMenuDao.findAll();
    }
    @Override
    public Map<String,Object> queryMenuTrees() {
        //查询所有的根节
        List<TbMenu> allMenus =tbMenuDao.findAll();
        //遍历所有的Id
        List<Integer>allIds =new ArrayList<>();
        for(TbMenu mmmm:allMenus){
            allIds.add(mmmm.getId());
        }
        //根节点遍历(一级菜单)
        List<TbMenu>rootMenu=new ArrayList<>();
        for(TbMenu menu:allMenus){
            if(null==menu.getParent()||menu.getChildren().size()>0){
                rootMenu.add(menu);
            }
        }
        for(TbMenu me:rootMenu){
           me.setChildren(getChild(me,allMenus));
        }
        //转成树
        List<TreeData>rootTrees =new ArrayList<>();
        TreeData treeData;
        for(TbMenu mm:rootMenu){
            if(null==mm.getParent()){
                treeData =new TreeData();
                treeData.setTitle(mm.getName());
                treeData.setId(mm.getId());
                treeData.setChildren(getChildTree(mm));
                rootTrees.add(treeData);
            }

        }
        //树状图数据 rootTrees
        System.out.println("rootTrees=="+JSON.toJSON(rootTrees));
        //net.sf.json.JSONObject 将Map转换为JSON方法
        JsonConfig jsonConfig =new JsonConfig();
        jsonConfig.setExcludes(new String[]{"parent"});
        jsonConfig.setIgnoreDefaultExcludes(false);
        //设置策略
        jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
        JSONArray jsonArray =new JSONArray();
        for(int i=0;i<rootMenu.size();i++){
            JSONObject jsonObject= JSONObject.fromObject(rootMenu.get(i),jsonConfig);
            jsonArray.set(i,jsonObject);
        }
        Map mp =new HashMap();
        //树数据
        mp.put("rootTrees",rootTrees);
        //所有的Id值
        mp.put("allIds",allIds);

        return mp;
    }

    @Override
    public TbMenu findMenuById(Integer id) {
        return tbMenuDao.getById(id);
    }

    @Override
    public void addMenu(TbMenu tbMenu) {
        tbMenuDao.save(tbMenu);
    }

    /**
     * 获取子树
     * @param menu
     * @param rootMenu
     * @return
     */
   List<TbMenu> getChild(TbMenu menu,List<TbMenu>rootMenu){
       List<TbMenu>childList =new ArrayList<>();
       //遍历所有节点与父节点比较
       for(TbMenu me:rootMenu){
           if(null!=me.getParent()){
               if(me.getParent().getId().equals(menu.getId())){
                   childList.add(me);
               }
           }
       }
       //递归遍历孩子的孩子节点
       for(TbMenu m:childList){
           if(!StringUtils.isEmpty(m.getUrl())){
               menu.setChildren(getChild(m,rootMenu));
           }
       }if(childList.size()==0){
           return null;
       }//退出递归
       return childList;
    }
    /**
     * 获取子树的子树
     */
    public List<TreeData> getChildTree(TbMenu tbMenu){
        List<TreeData> childList =new ArrayList<>();
        //如果等于0返回空
        if(null==tbMenu.getChildren()||tbMenu.getChildren().size()==0){
            return  new ArrayList<>();
        }
        //否则
       for(TbMenu m:tbMenu.getChildren()){
           TreeData treeData =new TreeData();
           treeData.setId(m.getId());
           treeData.setTitle(m.getName());
           if((null!=m.getChildren())&&m.getChildren().size()>0){
               if (m.getName().equals("子菜单21"))
                   System.out.println("len=="+m.getChildren().size());
              treeData.setChildren(getChildTree(m));
           }
           childList.add(treeData);
       }
        return childList;

    }
}
